package com.eleadin.ruleengine.context;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.NamedThreadLocal;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class FeignContextHolder {
    public static final String USER_ID = "x-user-id";
    public static final String USER_NAME = "x-user-name";
    public static final String ORG_ID = "x-org-id";
    public static final String SITE_CODE = "x-site-code";
    public static final String CHANNEL_CODE = "x-channel-code";

    private static final ThreadLocal<Map<String, Object>> CONTEXT = new NamedThreadLocal<Map<String, Object>>("Feign 上下文") {
        @Override
        protected Map<String, Object> initialValue() {
            return new HashMap<>();
        }
    };

    public static void bind(String key, Object value) {
        Objects.requireNonNull(key);
        if(value==null){
            return;
        }
        Map<String, Object> context = CONTEXT.get();
        context.put(key, value);
        CONTEXT.set(context);
    }

    public static <T> T get(String key) {
        return (T) CONTEXT.get().get(key);
    }

    public static Map<String, Object> get() {
        return CONTEXT.get();
    }

    private static void remove(String key) {
        Objects.requireNonNull(key);
        Map<String, Object> context = CONTEXT.get();
        context.remove(key);
        CONTEXT.set(context);
    }

    public static void clear() {
        Map<String, Object> context = CONTEXT.get();
        context.clear();
        CONTEXT.set(context);
    }

    public static void bindUserId(String userId) {
        bind(USER_ID, userId);
    }

    public static String getUserId() {
        return get(USER_ID);
    }

    public static void removeUserId() {
        remove(USER_ID);
    }

    public static void bindUserName(String userName) {
        bind(USER_NAME, userName);
    }

    public static String getUserName() {
        return get(USER_NAME);
    }

    public static void removeUserName() {
        remove(USER_NAME);
    }

    public static void bindOrgId(String orgId) {
        if(StringUtils.isEmpty(orgId)){
            return;
        }

        Long id = 0L;
        try {
            id = Long.valueOf(orgId);
        }catch (NumberFormatException e) {
            log.error("orgId格式转换错误：" + orgId);
            return;
        }
        bind(ORG_ID, id);
    }

    public static Long getOrgId() {
        return get(ORG_ID);
    }

    public static void removeOrgId() {
        remove(ORG_ID);
    }

    public static void bindSiteCode(String siteCode) {
        if(StringUtils.isEmpty(siteCode)){
            return;
        }

        Integer id = 0;
        try {
            id = Integer.valueOf(siteCode);
        }catch (NumberFormatException e) {
            log.error("siteCode格式转换错误：" + siteCode);
            return;
        }
        bind(SITE_CODE, id);
    }

    public static Integer getSiteCode() {
        return get(SITE_CODE);
    }

    public static void removeSiteCode() {
        remove(SITE_CODE);
    }

    public static void bindChannelCode(String channelCode) {
        if(StringUtils.isEmpty(channelCode)){
            return;
        }

        Integer id = 0;
        try {
            id = Integer.valueOf(channelCode);
        }catch (NumberFormatException e) {
            log.error("channelCode格式转换错误：" + channelCode);
            return;
        }
        bind(CHANNEL_CODE, id);
    }

    public static Integer getChannelCode() {
        return get(CHANNEL_CODE);
    }

    public static void removeChannelCode() {
        remove(CHANNEL_CODE);
    }
}
